<?php
class News_Form_ValidateExel{
	//Chứa những thông báo lỗi của form
	protected $_messagesError;
		
	//Mảng chứa dữ liệu sau khi kiểm tra
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null){
				
		//Kiểm tra File upload
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('fileexel');
		$fileName = $fileInfo['fileexel']['name'];		
		if(!empty($fileName)){
		
			$upload->addValidator('Extension',true,array('xls','xlm'),'fileexel');
			$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'fileexel');
			if(!$upload->isValid('fileexel')){
				$message = $upload->getMessages();
				$this->_messagesError['fileexel'] = 'File Exel : ' . current($message);
				
			}
		}	
		$this->_arrData = $arrParam;
	}
	
	public function isError(){
		//kiểm tra nếu có lỗi trả về giá trị true
		if(count($this->_messagesError) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function getMessageError($options = null){
		if($options['upload'] == true){
			$this->_arrData['fileexel'] = $this->uploadFile('fileexel',null);
		}
		return $this->_messagesError;
	}
	public function getData($options = null){
		if($options['upload'] == true){
			$this->_arrData['fileexel'] = $this->uploadFile('fileexel',null);
		}
		return $this->_arrData;
	}
	public function uploadFile($filename = null, $options = null){
		//Duong dan den thu muc upload		
		$upload_dir = FILES_PATH . '/exels/';
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo($filename);
		$fileName = $fileInfo[$filename]['name'];
		if(!empty($fileName)){
			$fileName = $upload->upload($filename, $upload_dir,null,$filename);
		}		
		return $fileName;		
	}
}